package com.hualan.sfzy.service.impl.permission;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.hualan.sfzy.constant.CommonConstant;
import com.hualan.sfzy.mapper.permission.PointsMapper;
import com.hualan.sfzy.mapper.permission.SystemUsersMapper;
import com.hualan.sfzy.mapper.permission.UserRoleMapper;
import com.hualan.sfzy.model.entity.Points;
import com.hualan.sfzy.model.entity.SystemUsers;
import com.hualan.sfzy.model.entity.UserRole;
import com.hualan.sfzy.service.BaseService;
import com.hualan.sfzy.utils.MD5Util;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class SystemUsersServiceImpl implements BaseService<SystemUsers> {

    @Autowired
    private SystemUsersMapper systemUsersMapper;
    @Autowired
    private UserRoleMapper userRoleMapper;
    @Autowired
    private PointsMapper pointsMapper ;


    @Override
    @Transactional
    public int add(SystemUsers systemUsers) {
        String encode = MD5Util.encode(systemUsers.getUserpwd());
        systemUsers.setUserpwd(encode);
        systemUsersMapper.insert(systemUsers);// 添加用户
        Integer id = systemUsers.getId();
        if(id==null){
            throw new RuntimeException("用户添加失败");
        }
        UserRole role = new UserRole();
        role.setUserId(id);
        role.setRoleId(2);// 普通用户
        int i = userRoleMapper.insert(role);// 添加用户角色关系
        if(i<=0){
            throw new RuntimeException("用户角色关系添加失败");
        }
        Points points = new Points();
        points.setUser_id(id);
        int i1 = pointsMapper.insert(points);// 添加用户积分
        if(i1<=0){
            throw new RuntimeException("用户积分添加失败");
        }
        return 1;


    }

    @Override
    public int edit(SystemUsers systemUsers) {
        return systemUsersMapper.update(systemUsers);
    }

    @Override
//    @Transactional
    public int remove(Integer id) {
//        // 需要在用户角色中间表删除用户角色关系
//        int remove = userRoleMapper.delete(id);
//        if(remove<=0){
//            throw new RuntimeException("用户角色关系删除失败");
//        }
//        // 删除用户积分
//        int remove1 = pointsMapper.delete(id);
//        if(remove1<=0){
//            throw new RuntimeException("用户积分删除失败");
//        }
        return systemUsersMapper.delete(id);
    }

    @Override
    public int removeBatch(Integer[] ids) {
        return systemUsersMapper.deleteBatch(ids);
    }

    @Override
    public List<SystemUsers> find() {
        return systemUsersMapper.select(null);
    }

    @Override
    public Map<String, Object> findPage(Integer pageNum, Integer pageSize, SystemUsers systemUsers) {
        PageHelper.startPage(pageNum, pageSize);
        List<SystemUsers> list = systemUsersMapper.select(systemUsers);
        PageInfo<SystemUsers> pageInfo = new PageInfo<>(list);
        Map<String, Object> pageMap = new HashMap<>();
       pageMap.put(CommonConstant.PAGE_LIST , list);
       pageMap.put(CommonConstant.PAGE_TOTAL , pageInfo.getPages());
       pageMap.put(CommonConstant.TOTAL, pageInfo.getTotal());
        return pageMap;
    }

    @Override
    public SystemUsers findById(Integer id) {
        return systemUsersMapper.selectById(id);
    }

    // 根据用户精确查询
    public SystemUsers findByUsername(String username) {
        return systemUsersMapper.selectByUsername(username);
    }

    // 修改密码
    public int updatePwd(SystemUsers systemUsers) {
        String encode = MD5Util.encode(systemUsers.getUserpwd());
        systemUsers.setUserpwd(encode);
        return systemUsersMapper.updatePwd(systemUsers);
    }

    @Transactional
    public SystemUsers forGetPwd(SystemUsers systemUsers) {
        SystemUsers users = systemUsersMapper.forGetPwd(systemUsers);
        if(users==null){
            throw new RuntimeException("您输入的有误");
        }else{
            String encode = MD5Util.encode(CommonConstant.PASSWORD_DEFAULT);
            users.setUserpwd(encode);
            return systemUsersMapper.updatePwd(users)>0?users:null;

        }
    }
}
